package cuiying.rbac.service.array;

import cuiying.rbac.domain.dto.DeptDto;
import cuiying.rbac.domain.dto.RoleDto;
import cuiying.rbac.domain.entity.Dept;
import cuiying.rbac.domain.entity.Role;
import cuiying.rbac.domain.vo.DeptVo;
import cuiying.rbac.domain.vo.RoleVo;
import cuiying.rbac.domain.vo.Result;
import cuiying.rbac.service.IRoleService;
import cuiying.rbac.service.IUserService;

import java.util.Arrays;

/**
 * 业务层-部门
 *
 * @author 崔颖
 * @since 2025/4/20 15:33
 */
public abstract class RoleService extends BaseService<Role, RoleDto,RoleVo>
        implements IRoleService {


    private IUserService userService;


    private static IRoleService singleton;
    private  RoleService(){
        data = new Role[DATA_SIZE];
        temp = new RoleVo[ DATA_SIZE];
    }
    public static IRoleService getInstance(){
        if(singleton == null){
            singleton = new RoleService();
        }
        return singleton;
    }

    @Override
    RoleVo convertToVo(Role Role){
        return new RoleVo(Role.getId(),Role.name,Role.datascope);
    }

    @Override
    Role createEntity(RoleDto dto,int id){

        return new Role(id,dto.name,dto.datascope);
    }

    public void updataEntity(RoleDto dto,Role Role){
        Role.name = dto.name;
        Role.datascope = dto.datascope;
    }



    @Override
    public Result delete(int id){
        if(!existsById(id)){
            return Result.failure("删除失败，不存在该数据");
        }
        if(userService == null){
            userService = UserService.getInstance();
        }
        if(userService.roleInUse(id)){
            return Result.failure("删除失败，还有用户具有该角色，不能删除");
        }
        data[id] = null;
        return Result.success("删除成功",null);
    }
}
